/**
 * Generali Solutions d'assurances - Tous droits reserves &copy; 2007 - 2010
 */
package fr.generali.accueilclient.xref.assembler;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import fr.generali.accueilclient.client.om.IIdentifiantsXReference;
import fr.generali.accueilclient.client.om.IListeIdentifiantsXReference;
import fr.generali.accueilclient.client.om.IXReference;
import fr.generali.accueilclient.client.om.impl.IdentifiantsXReference;
import fr.generali.accueilclient.client.om.impl.ListeIdentifiantsXReference;
import fr.generali.accueilclient.client.om.impl.XReference;
import fr.generali.accueilclient.utils.spel.mapping.Mapper;
import fr.generali.refservcommuns.rce.consultationxref.dtoxref.DtoDossierXRef;
import fr.generali.refservcommuns.rce.consultationxref.dtoxref.DtoListeXRef;
import fr.generali.refservcommuns.rce.consultationxref.dtoxref.XRef;

/**
 * The Class ConsultationXReferenceMapper.
 * 
 * @author Holmes Kelly
 */

@Component("consultationXReferenceMapper")
public class ConsultationXReferenceMapper {

    /**
     * Mapper liste identifiants x reference2 dto liste x ref.
     * 
     * @param identifiantsXReference the identifiants x reference
     * @return the dto liste x ref
     */
    public DtoListeXRef mapperListeIdentifiantsXReference2DtoListeXRef(
                    IListeIdentifiantsXReference identifiantsXReference) {

        DtoListeXRef dtoListeXRef = null;

        if (identifiantsXReference != null) {

            dtoListeXRef = new DtoListeXRef();

            mapIListeIdentifiantsXReferenceToDtoListeXRef().map(identifiantsXReference, dtoListeXRef);

        }

        return dtoListeXRef;

    }

    /**
     * Mapper dto liste x ref2 i liste identifiants x reference.
     * 
     * @param dtoListeXRef the dto liste x ref
     * @return the i liste identifiants x reference
     */
    public IListeIdentifiantsXReference mapperDtoListeXRef2IListeIdentifiantsXReference(DtoListeXRef dtoListeXRef) {

        IListeIdentifiantsXReference identifiantsXReference = null;

        if (dtoListeXRef != null) {

            identifiantsXReference = new ListeIdentifiantsXReference();

            mapDtoListeXRefToIListeIdentifiantsXReference().map(dtoListeXRef, identifiantsXReference);
        }

        return identifiantsXReference;
    }

    /**
     * Map i liste identifiants x reference to dto liste x ref.
     * 
     * @return the mapper
     */
    private Mapper<IListeIdentifiantsXReference, DtoListeXRef> mapIListeIdentifiantsXReferenceToDtoListeXRef() {

        Mapper<IListeIdentifiantsXReference, DtoListeXRef> map =
                        new Mapper<IListeIdentifiantsXReference, DtoListeXRef>() {

                            @Override
                            public DtoListeXRef map(IListeIdentifiantsXReference source, DtoListeXRef target) {

                                if (source != null) {

                                    if (CollectionUtils.isNotEmpty(source.getAdresse())) {

                                        target.getAdresse().addAll(
                                                        converterIIdentifiantsXReference2DtoDossierXRef().convert(
                                                                        source.getAdresse()));
                                    }

                                    if (CollectionUtils.isNotEmpty(source.getPersonne())) {

                                        target.getPersonne().addAll(
                                                        converterIIdentifiantsXReference2DtoDossierXRef().convert(
                                                                        source.getPersonne()));
                                    }

                                    if (CollectionUtils.isNotEmpty(source.getRib())) {

                                        target.getRib().addAll(
                                                        converterIIdentifiantsXReference2DtoDossierXRef().convert(
                                                                        source.getRib()));
                                    }

                                }

                                return target;
                            }

                        };

        return map;
    }

    /**
     * Map dto liste x ref to i liste identifiants x reference.
     * 
     * @return the mapper
     */
    private Mapper<DtoListeXRef, IListeIdentifiantsXReference> mapDtoListeXRefToIListeIdentifiantsXReference() {

        Mapper<DtoListeXRef, IListeIdentifiantsXReference> map =
                        new Mapper<DtoListeXRef, IListeIdentifiantsXReference>() {

                            @Override
                            public IListeIdentifiantsXReference map(DtoListeXRef source,
                                            IListeIdentifiantsXReference target) {

                                if (source != null) {

                                    if (CollectionUtils.isNotEmpty(source.getAdresse())) {

                                        target.setAdresse(converterDtoDossierXRef2IIdentifiantsXReference().convert(
                                                        source.getAdresse()));
                                    }

                                    if (CollectionUtils.isNotEmpty(source.getPersonne())) {

                                        target.setPersonne(converterDtoDossierXRef2IIdentifiantsXReference().convert(
                                                        source.getPersonne()));
                                    }

                                    if (CollectionUtils.isNotEmpty(source.getRib())) {

                                        target.setRib(converterDtoDossierXRef2IIdentifiantsXReference().convert(
                                                        source.getRib()));
                                    }

                                }

                                return target;
                            }
                        };

        return map;
    }

    /**
     * Converter i identifiants x reference2 dto dossier x ref.
     * 
     * @return the converter
     */
    private Converter<List<IIdentifiantsXReference>, List<DtoDossierXRef>> converterIIdentifiantsXReference2DtoDossierXRef() {

        Converter<List<IIdentifiantsXReference>, List<DtoDossierXRef>> converter =
                        new Converter<List<IIdentifiantsXReference>, List<DtoDossierXRef>>() {

                            @Override
                            public List<DtoDossierXRef> convert(List<IIdentifiantsXReference> source) {

                                List<DtoDossierXRef> listeDtoDossierXRefs = null;

                                if (CollectionUtils.isNotEmpty(source)) {

                                    listeDtoDossierXRefs = new ArrayList<DtoDossierXRef>(0);

                                    CollectionUtils.collect(source,
                                                    getTransformerIIdentifiantsXReference2DtoDossierXRef(),
                                                    listeDtoDossierXRefs);

                                }

                                return listeDtoDossierXRefs;
                            }
                        };

        return converter;
    }

    /**
     * Converter dto dossier x ref2 i identifiants x reference.
     * 
     * @return the converter
     */
    private Converter<List<DtoDossierXRef>, List<IIdentifiantsXReference>> converterDtoDossierXRef2IIdentifiantsXReference() {

        Converter<List<DtoDossierXRef>, List<IIdentifiantsXReference>> converter =
                        new Converter<List<DtoDossierXRef>, List<IIdentifiantsXReference>>() {

                            @Override
                            public List<IIdentifiantsXReference> convert(List<DtoDossierXRef> source) {

                                List<IIdentifiantsXReference> listeIdentifiantsXReferences = null;

                                if (CollectionUtils.isNotEmpty(source)) {

                                    listeIdentifiantsXReferences = new ArrayList<IIdentifiantsXReference>(0);

                                    CollectionUtils.collect(source,
                                                    getTransformerDtoDossierXRef2IIdentifiantsXReference(),
                                                    listeIdentifiantsXReferences);
                                }

                                return listeIdentifiantsXReferences;
                            }
                        };

        return converter;
    }

    /**
     * Converter ix reference2 x ref.
     * 
     * @return the converter
     */
    private Converter<IXReference, XRef> converterIXReference2XRef() {

        Converter<IXReference, XRef> converter = new Converter<IXReference, XRef>() {

            @Override
            public XRef convert(IXReference source) {

                XRef xRef = null;

                if (source != null) {

                    xRef = new XRef();

                    xRef.setId(source.getId());
                    xRef.setSystemeCodifiant(source.getSystemeCodifiant());
                }

                return xRef;
            }
        };

        return converter;
    }

    /**
     * Converter x ref2 ix reference.
     * 
     * @return the converter
     */
    private Converter<XRef, IXReference> converterXRef2IXReference() {

        Converter<XRef, IXReference> converter = new Converter<XRef, IXReference>() {

            @Override
            public IXReference convert(XRef source) {

                IXReference xReference = null;

                if (source != null) {

                    xReference = new XReference();

                    xReference.setId(source.getId());
                    xReference.setSystemeCodifiant(source.getSystemeCodifiant());
                }

                return xReference;
            }
        };

        return converter;
    }

    /**
     * Gets the transformer i identifiants x reference2 dto dossier x ref.
     * 
     * @return the transformer i identifiants x reference2 dto dossier x ref
     */
    private Transformer getTransformerIIdentifiantsXReference2DtoDossierXRef() {

        Transformer transformer = new Transformer() {

            @Override
            public Object transform(Object input) {

                IIdentifiantsXReference identifiantsXReference = IIdentifiantsXReference.class.cast(input);

                DtoDossierXRef dossierXRef = new DtoDossierXRef();

                dossierXRef.setReference(converterIXReference2XRef().convert(identifiantsXReference.getReference()));
                dossierXRef.setReferenceRCE(converterIXReference2XRef().convert(
                                identifiantsXReference.getReferenceRCE()));

                if (CollectionUtils.isNotEmpty(identifiantsXReference.getxRef())) {

                    List<XRef> listXRefs = new ArrayList<XRef>(0);

                    CollectionUtils.collect(identifiantsXReference.getxRef(), getTransformerIXReference2XRef(),
                                    listXRefs);

                    dossierXRef.getXRef().addAll(listXRefs);

                }

                return dossierXRef;
            }
        };

        return transformer;
    }

    /**
     * Gets the transformer dto dossier x ref2 i identifiants x reference.
     * 
     * @return the transformer dto dossier x ref2 i identifiants x reference
     */
    private Transformer getTransformerDtoDossierXRef2IIdentifiantsXReference() {

        Transformer transformer = new Transformer() {

            @Override
            public Object transform(Object input) {

                DtoDossierXRef dtoDossierXRef = DtoDossierXRef.class.cast(input);

                IIdentifiantsXReference identifiantsXReference = new IdentifiantsXReference();

                identifiantsXReference.setReference(converterXRef2IXReference().convert(dtoDossierXRef.getReference()));
                identifiantsXReference.setReferenceRCE(converterXRef2IXReference().convert(
                                dtoDossierXRef.getReferenceRCE()));

                if (CollectionUtils.isNotEmpty(dtoDossierXRef.getXRef())) {

                    List<IXReference> listXReferences = new ArrayList<IXReference>(0);

                    CollectionUtils.collect(dtoDossierXRef.getXRef(), getTransformerXRef2IXReference(), listXReferences);

                    identifiantsXReference.setxRef(listXReferences);

                }

                return identifiantsXReference;
            }
        };

        return transformer;
    }

    /**
     * Gets the transformer x ref2 ix reference.
     * 
     * @return the transformer x ref2 ix reference
     */
    private Transformer getTransformerXRef2IXReference() {

        Transformer transformer = new Transformer() {

            @Override
            public Object transform(Object input) {

                XRef xRef = XRef.class.cast(input);

                IXReference xReference = converterXRef2IXReference().convert(xRef);

                return xReference;
            }
        };

        return transformer;
    }

    /**
     * Gets the transformer ix reference2 x ref.
     * 
     * @return the transformer ix reference2 x ref
     */
    private Transformer getTransformerIXReference2XRef() {

        Transformer transformer = new Transformer() {

            @Override
            public Object transform(Object input) {

                IXReference xReference = IXReference.class.cast(input);

                XRef xRef = converterIXReference2XRef().convert(xReference);

                return xRef;
            }
        };

        return transformer;

    }

}
